import React, { useState } from "react";
import { Form, Button, Input, NavBar, Toast } from "antd-mobile";
import { useNavigate } from "react-router-dom";
import { getReg } from "../../api/info";
function Reg() {
  let nav = useNavigate();
  let [form] = Form.useForm();
  let [flag, setFlag] = useState(true);
  const mm = () => {
    let m = form.validateFields(["password"]);
    if (m.length !== 0) {
      setFlag(false);
    }
  };
  const onFinish = async (values) => {
    console.log(values);
    await getReg(values);
    Toast.show({
      icon: "success",
      content: "注册成功",
    });
    nav("/login");
  };
  return (
    <div>
      <NavBar />
      <h1
        style={{ textAlign: "center", padding: "20px", fontWeight: "normal" }}
      >
        注册账户
      </h1>
      <Form
        form={form}
        onFinish={onFinish}
        style={{
          padding: "15px",
          "--border-bottom": "none",
          "--border-top": "none",
          "--prefix-width": "75px",
        }}
        layout="horizontal"
        footer={
          <Button
            disabled={flag}
            block
            type="submit"
            color="danger"
            size="large"
          >
            注册
          </Button>
        }
      >
        <Form.Item
          name="name"
          rules={[{ required: true, message: "姓名不能为空" }]}
        >
          <Input placeholder="用户名" />
        </Form.Item>
        <Form.Item
          name="username"
          rules={[
            { required: true, message: "电子邮箱不能为空" },
            {
              pattern: /^\d{3,8}@(163.com||qq.com)$/,
              message: "网易邮箱或qq邮箱",
            },
          ]}
        >
          <Input placeholder="电子邮件" />
        </Form.Item>
        <Form.Item
          name="phone"
          rules={[{ required: true, message: "电话不能为空" }]}
        >
          <Input placeholder="电话" />
        </Form.Item>
        <Form.Item
          name="date"
          rules={[{ required: true, message: "出生日期不能为空" }]}
        >
          <Input placeholder="出生日期" />
        </Form.Item>
        <Form.Item
          name="password"
          rules={[{ required: true, message: "密码不能为空" }]}
        >
          <Input onChange={mm} placeholder="密码" />
        </Form.Item>
      </Form>
      <p style={{ paddingTop: "120px", textAlign: "center" }}>
        点击注册表示您毫无保留地同意条款和条件
      </p>
    </div>
  );
}

export default Reg;
